package com.jnxg.smartcommunity.activity.user;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.geekbean.android.utils.GB_StringUtils;
import com.geekbean.android.utils.GB_VerificationUtils;
import com.jnxg.smartcommunity.R;
import com.jnxg.smartcommunity.activity.BaseActivity;
import com.jnxg.smartcommunity.activity.MainActivity;
import com.jnxg.smartcommunity.contas.Contas;
import com.jnxg.smartcommunity.manager.UrlManager;
import com.jnxg.smartcommunity.responseutils.ResponseUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONObject;

import static com.jnxg.smartcommunity.R.id.tv_findPass;

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv_goReg;
    private SharedPreferences sp;
    private EditText mobileEdit;
    private EditText passwordEdit;
    private static final String FILE_NAME = "messageNew";
    private String mobile,pass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_login);
        initView();
    }
    public void initView(){

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar 这是顶部的沉浸式
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        // Translucent navigation bar 这是底部的沉浸式
////            window.setFlags(
////                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
////                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        findViewById(R.id.btn_login).setOnClickListener(this);
        findViewById(R.id.tv_goReg).setOnClickListener(this);
        findViewById(tv_findPass).setOnClickListener(this);
        mobileEdit = (EditText) findViewById(R.id.edit_mobile);
        passwordEdit = (EditText) findViewById(R.id.edit_pass);
    }

    @Override
    public void onClick(View arg0) {
        if(arg0.getId()==R.id.tv_findPass){
            Intent intent = new Intent(LoginActivity.this,FindPassActivity.class);
            startActivity(intent);
        }
        if(arg0.getId()==R.id.tv_goReg){
          Intent intent = new Intent(LoginActivity.this,RegActivity.class);
          startActivity(intent);
      }
        if (arg0.getId() == R.id.btn_login) {
            mobile = mobileEdit.getText().toString().trim();
            pass = passwordEdit.getText().toString().trim();
            if (GB_StringUtils.isBlank(mobile)
                    || !GB_VerificationUtils.isMobilePhoneNumber(mobile)) {
                Toast.makeText(this, "手机号错误", Toast.LENGTH_SHORT).show();
                return;
            }
            if (GB_StringUtils.isBlank(pass)) {
                Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }
            logins();
        }
    }

    public void logins(){
        String url = UrlManager.getInstance().memberLogin();
        OkHttpUtils
                .post()
                .url(url)
                .addParams("loginname", mobile)
                .addParams("password", pass)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(okhttp3.Call call, Exception e, int i) {
                        Toast.makeText(getBaseContext(), "请检查网络", Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onResponse(String s, int i) {
                        Log.i("tag",s);
                        if(ResponseUtils.getCode(s)==1)  {
                            try {
                                JSONObject obj = new  JSONObject(s);
                                JSONObject data = obj.getJSONObject("data");
                                String token =data.getString("token");
                                Toast.makeText(getBaseContext(), "登陆成功", Toast.LENGTH_SHORT).show();
                                Contas.putToken(LoginActivity.this,token);
                                String userid =data.getString("userid");
                                Contas.putUserId(LoginActivity.this,userid);
                                String mobile =data.getString("mobile");
                                Contas.putPhone(LoginActivity.this,mobile);
                                Intent intent = new  Intent(LoginActivity.this, MainActivity.class);
                                startActivity(intent);
                                finish();
                            }catch (Exception e){
                            }
                        }else{
                            Toast.makeText(getBaseContext(), ResponseUtils.getMsg(s), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
}
